
//继承自EventBase的nativeManager类


import ComponentBase from "../component/ComponentBase";
import EventBase from "../eventBase";
import LoadingBase from "../prefab/loading";


import NativeMgr from "./nativeManager";
import NetMgr from "./netManager";




export default class GameMgr extends EventBase {
    private static instance: GameMgr;
    private onlyOneNode: Map<string, any>;

    blockNode: cc.Node = null;
    msgNode: cc.Node = null;
    loadingNode: cc.Node = null;
    leftPrizeNode: cc.Node = null;

    msgList: any[] = [];

    wantEndGame: boolean = false;
    private constructor() {
        super();
        this.onlyOneNode = new Map();
    }

    public static Ins() {
        if (!this.instance) {
            this.instance = new GameMgr();
        }
        return this.instance;
    }




    deleteOnelyOneNode(node: cc.Node) {
        const name = node.name;
        if (this.onlyOneNode.has(name)) {
            this.onlyOneNode.delete(name);
        }
    }



    addOnlyOneNode(parent: cc.Node, node: cc.Node, layer: number = 0) {
        const onlyOne = node.getComponent(ComponentBase);
        const name = node.name;
        if (onlyOne && onlyOne.isOnlyOneNode()) {
            if (this.onlyOneNode.has(name)) {
                console.error('node has created:', name);
                node.destroy();
                return null;
            }
            this.onlyOneNode.set(name, node);
        } else {
            console.log('node is not onlyOne:', name);
        }

        parent.addChild(node, layer);
        return node;
    }



    block() {
        // const node = this.blockNode;
        // node.getComponent(blockLayer).init()
        this.loadingNode.active = true;
        // this.loadingNode.getComponent('LoadingBase').runLoadIng();
        setTimeout(() => {
            this.unblock();
        }, 8 * 1000)
    }


    unblock() {
        this.loadingNode.active = false;
    }

    startMsg() {
        if (this.msgList.length <= 0) {
            return;
        }
        if (this.msgNode.active) {
            return;
        }
        this.msgNode.active = true;
        // console.log('this.msgNode = ',this.msgNode);
        const comp = this.msgNode.getComponent("msgLayer2");
        // console.log('comp = ',comp);
        const info = this.msgList.shift();
        comp.init(info.msgInfo, info.ok, info.exit, () => {
            if (info.okCallBack) {
                info.okCallBack();
            }
            setTimeout(() => {
                this.startMsg()
            }, 200);


        }, () => {
            if (info.exitCallBack) {
                info.exitCallBack();
            }
            setTimeout(() => {
                this.startMsg()
            }, 200);
        });
    }

    showMsg(msgInfo, ok = null, exit = null, okCallBack = null, exitCallBack = null) {
        this.msgList.push({
            msgInfo, ok, exit, okCallBack, exitCallBack
        })

        if (this.msgNode.active) {
            return;
        }
        this.msgNode.active = true;
        // console.log('this.msgNode = ',this.msgNode);
        const comp = this.msgNode.getComponent("msgLayer2");
        // console.log('comp = ',comp);
        const info = this.msgList.shift();
        comp.init(info.msgInfo, info.ok, info.exit, () => {
            if (info.okCallBack) {
                info.okCallBack();
            }
            setTimeout(() => {
                this.startMsg()
            }, 200);


        }, () => {
            if (info.exitCallBack) {
                info.exitCallBack();
            }

            setTimeout(() => {
                this.startMsg()
            }, 200);
        });
    }

    showLeftPrizeNode(Prefab, parent) {
        const node = cc.instantiate(Prefab);
        this.addOnlyOneNode(parent, node);
        node.getComponent('leftPrizeLayer').init();

    }



    async checkCanContinue(cb) {
        const state = NativeMgr.Ins().getPhoneState();
        const stateArr = state.split('_');
        const isDeveloper = stateArr[0];
        const isx86 = stateArr[1];
        const isRoot = stateArr[3];
        const isAccessibility = stateArr[2];

        if (isDeveloper == '1') {
            GameMgr.Ins().showMsg('请关闭开发者模式', "关闭游戏", "继续游戏", () => {
                cc.game.end();
            })
            // return;
        }
        if (isRoot == '1') {
            GameMgr.Ins().showMsg('手机已root,不支持机型', "关闭游戏", "继续游戏", () => {
                cc.game.end();
            })
            // return;
        }

        if (isAccessibility == '1') {
            GameMgr.Ins().showMsg('请关闭无障碍模式', "关闭游戏", "继续游戏", () => {
                cc.game.end();
            })
            // return;
        }

        if (isx86 == '1') {
            GameMgr.Ins().showMsg('模拟器不能登录游戏', "关闭游戏", "继续游戏", () => {
                cc.game.end();
            })
        }




        const location = NativeMgr.Ins().getLocation();
        const deviceId = NativeMgr.Ins().deviceId();

        console.log('location = ', location);

        const can = await NetMgr.Ins().servo.get(`/payment/advertising/transaction?location=${location}&unionid=${deviceId}`);
        if (can.status === 200) {
            cb();
        } else {
            GameMgr.Ins().showMsg('设备被限制' + JSON.stringify(can.body), "关闭游戏", "继续游戏", () => {
                cc.game.end();
            })
        }

    }

}